package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Bill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dragon.repository.entity.PlanCollect;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * <p>
 * 账单表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-12-12
 */
@Mapper
public interface BillMapper extends BaseMapper<Bill> {


    @Select(" SELECT M.* FROM ( SELECT db.* , dc.customer_name as customerName, dbc.accrued_plan_money as accruedPlanMoney , " +
            "dbc.accrued_payment_money as accruedPaymentMoney, dc.admin_id as customerAdminId, dc.department_id as customerDepartmentId FROM dragon_bill db " +
            "LEFT JOIN dragon_customer dc on db.customer_id = dc.id " +
            "LEFT JOIN dragon_bill_customer dbc on db.customer_id = dbc.customer_id )M "+
            "${ew.customSqlSegment} " )
    Page<Bill> selectPageByWrapper(Page<Bill> page1, @Param(Constants.WRAPPER) QueryWrapper<Bill> wrapper);

    @Select("SELECT db.* , dc.customer_name as customerName, dbc.accrued_plan_money as accruedPlanMoney , " +
            "dbc.accrued_payment_money as accruedPaymentMoney FROM dragon_bill db " +
            "LEFT JOIN dragon_customer dc on db.customer_id = dc.id " +
            "LEFT JOIN dragon_bill_customer dbc on db.customer_id = dbc.customer_id where db.id = #{billId} ")
    Bill selectBillOne(Integer billId);

    @Select("select sum(payment_money) FROM dragon_bill " +
            "${ew.customSqlSegment}")
    Double selectPaymentMoney(@Param(Constants.WRAPPER) QueryWrapper<Bill> wrapper);

    @Select("select sum(plan_money) FROM dragon_bill " +
            "${ew.customSqlSegment}")
    Double selectPlanMoney(@Param(Constants.WRAPPER) QueryWrapper<Bill> wrapper);
}
